package data.component;

import java.util.Arrays;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class BoundingBox {
    public Double minX;
    public Double minY;
    public Double maxX;
    public Double maxY;
    public Double[] min;
    public Double[] max;

    public Double[] leftUp;

    public Double[] rightDw;

    public Double[] center;
    public Double[] size;

    public BoundingBox(List<Double[]> points) {
        var xArrayList = points.stream().map(p -> p[0]).collect(Collectors.toList());
        var yArrayList = points.stream().map(p -> p[1]).collect(Collectors.toList());
        this.maxX = Collections.max(xArrayList);
        this.minX = Collections.min(xArrayList);
        this.maxY = Collections.max(yArrayList);
        this.minY = Collections.min(yArrayList);
        this.min = new Double[]{this.minX, this.minY};
        this.max = new Double[]{this.maxX, this.maxY};
        this.leftUp = new Double[] {this.minX, this.maxY};
        this.rightDw = new Double[] { this.maxX, this.minY};
        this.center = new Double[] {(this.minX + this.maxX)/2, (this.minY + this.maxY)/2};
        this.size = new Double[]{this.maxX - this.minX, this.maxY - this.minY};
    }

    public static BoundingBox Merge(BoundingBox b1, BoundingBox b2) {
        var pMin = new Double[]{Math.min(b1.minX, b2.minX), Math.min(b1.minY, b2.minY)};
        var pMax = new Double[]{Math.max(b1.maxX, b2.maxX), Math.max(b1.maxY, b2.maxY)};
        var aList = new ArrayList<Double[]>(Arrays.asList(pMin, pMax));
        return new BoundingBox(aList);
    }
}
